package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //将数据插入套餐表中
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectSetmealPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            //状态为启售的无法删除
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException("启售状态无法删除");
            }
        }

        for (Long id : ids) {
            //删除setmeal表里的数据
            setmealMapper.delete(id);
            //删除setmeal_dish表里的数据
            setmealDishMapper.delete(id);
        }
    }

    /**
     * 根据id获取套餐
     * @param id
     */
    @Override
    public SetmealVO selectById(Long id) {
        //根据id查询套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据id查询套餐菜品关系表
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateSetmealWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐表数据
        setmealMapper.update(setmeal);
        //删除套餐菜品表数据
        setmealDishMapper.delete(setmealDTO.getId());
        //获取新输入的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //无需判断是否有重新输入的菜品(与更新菜品不同，前端页面已经设置必须输入数据)
        //将重新输入的菜品数据插入到套餐菜品表
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        //添加到套餐菜品表
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐启售、停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        //如果修改套餐为停售状态，则无需任何判断
        //如果修改套餐为启售状态，若套餐内有菜品停售，则套餐不可启售
        if (status == StatusConstant.ENABLE) {
            //根据id查询套餐菜品表用于获取菜品id
            List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
            for (SetmealDish setmealDish : setmealDishes) {
                Long dishId = setmealDish.getDishId();
                //通过菜品id查询到菜品对象获取启用禁用状态值
                Dish dish = dishMapper.selectById(dishId);
                Integer dishStatus = dish.getStatus();
                //判断菜品是否被停售
                if (dishStatus == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException("套餐启用失败异常");
                }
            }
        }
        //修改套餐状态
        Setmeal setmeal = Setmeal.builder().
                status(status).
                id(id).
                build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
